﻿using System;
using System.Drawing.Imaging;
using System.Text;

namespace Panos.Utils
{
    public static class PropertyItemExtensions
    {
        public static Angle GetAngle(this PropertyItem prop)
        {
            if (prop.Id != 2 && prop.Id != 4)
            {
                throw new ArgumentException("PropertyItem does not describe an angle", "prop");
            }
            // Specifies that Value data member is an array of pairs of unsigned long integers. 
            // Each pair represents a fraction; the first integer is the numerator and the second integer is the denominator.
            return new Angle()
            {
                Degree = (byte)BitConverter.ToUInt32(prop.Value, 0),
                Minute = (byte)BitConverter.ToUInt32(prop.Value, 8),
                Second = BitConverter.ToUInt32(prop.Value, 16) / (double)BitConverter.ToUInt32(prop.Value, 20)
            };
        }

        public static DateTime GetDateTime(this PropertyItem prop)
        {
            if (prop.Id != 0x0132)
            {
                throw new ArgumentException("PropertyItem does not describe an angle", "prop");
            }
            return DateTime.Parse(Encoding.ASCII.GetString(prop.Value).Split(' ')[0].Replace(':', '-'));
        }
    }
}